package com.aierkeji.b2c.dao.jpa.custom.Impl;

import org.springframework.transaction.annotation.Transactional;

import com.aierkeji.b2c.dao.jpa.custom.AkjAuserDaoCustom;
import com.aierkeji.b2c.utils.StringUtils;
import com.aierkeji.b2c.utils.WebUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.sql.Timestamp;

public class AkjAuserDaoCustomImpl implements AkjAuserDaoCustom {

    @PersistenceContext
    private EntityManager em;


    @Override
    @Transactional
    public Integer bindAlilife(String auid, String aliId,
                               String createBy, Timestamp crTime) {
        if(WebUtils.checkParamsIsNull(auid, aliId,createBy,crTime)){
            return 0;
        }
//        String checksSQL = " SELECT count(*) from akj_auser_alipay_ref where au_id=? and ali_id=? ";
//        Integer checkResult = (Integer) em.createNativeQuery(checksSQL).setParameter(1,auid).setParameter(2,aliId)
//                .getSingleResult();
//        if(checkResult == null || checkResult == 0){
//            return 0;
//        }
        String sql = "INSERT into akj_auser_alipay_ref(create_at,create_by,ali_id,au_id) " +
                "VALUES(:crTime,:createBy,:aliId,:auid)";
        Query query = em.createNativeQuery(sql);
        query.setParameter("crTime", crTime);
        query.setParameter("createBy", createBy);
        query.setParameter("auid", auid);
        query.setParameter("aliId", aliId);
        return query.executeUpdate();
    }

    @Override
    @Transactional
    public Integer deleteRelateLifes(String auid) {
        if(WebUtils.checkParamsIsNull(auid)){
            return 0;
        }
        String sql = " delete FROM akj_auser_alipay_ref where au_id = :auid ";
        Query query = em.createNativeQuery(sql);
        query.setParameter("auid", auid);
        return query.executeUpdate();
    }

}
